package com.rykj.qxj.rest;


import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.rykj.qxj.annotation.EnableSystemLog;
import com.rykj.qxj.server.model.entity.ApiVersionManage;
import com.rykj.qxj.server.services.IApiManageService;
import com.rykj.qxj.server.services.IApiRequestParamService;
import com.rykj.qxj.server.services.IApiResponseParamService;
import com.rykj.qxj.server.services.IApiVersionManageService;
import com.rykj.qxj.util.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;

@Log4j
@RestController
@RequestMapping("/v1/apiManage/apiVersionManage")
@Api(tags="接口管理-版本管理")
public class ApiVersionManageController {
	
	@Autowired
	private IApiVersionManageService apiVersionManageService;
	
	@GetMapping("/list")
    @ApiOperation(value="分页获取接口版本记录列表",notes ="分页获取接口版本记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "apiId", value = "接口的Id",required = true,paramType = "query"),
    })
    public Result getApiManageList(@RequestParam(value = "pageNum") Integer pageNum,@RequestParam(value = "pageSize") Integer pageSize,
                  @RequestParam(value = "apiId") Integer apiId){
		    if(apiId==null) {
		       return new Result(false, "查看接口版本的时候接口的id不能为空");
		          }
            try{
           PageInfo page = apiVersionManageService.selectApiVersionManagePageByApiId(pageNum, pageSize,apiId);
            return new Result(page);
        }catch (Exception e){
            log.error("查询接口版本记录列表失败", e);
            return new Result(false, "查询接口版本记录列表失败");
        }
    }
	@DeleteMapping("/delete")
    @ApiOperation(value="删除接口信息:逻辑删除接口的版本",notes ="删除接口信息:逻辑删除接口的版本")
	@EnableSystemLog(value="逻辑删除接口的版",fullPath ="/v1/apiManage/apiVersionManage/delete")
	@ApiImplicitParams({
         @ApiImplicitParam(name = "apiVersionId", value = "接口版本的Id",required = true,paramType = "query")
   })
	 public Result deleteApiManageById(@RequestParam Integer apiVersionId){
			if(apiVersionId==null) {
				return Result.failure("删除数据的时候Id不能为空");
			}
	        try{
	        	apiVersionManageService.deleteApiVersionManage(apiVersionId);
	            return new Result(true,"删除成功");
	        }catch (Exception e){
	            log.error("删除失败", e);
	            return new Result(false, "删除失败");
	        }
	    }
	@PutMapping("/enableOrDisableOperation")
	@ApiOperation(value="接口版本启用,停用操作",notes ="接口版本启用,停用操作")
	@EnableSystemLog(value="接口版本启用,停用操作",fullPath ="/v1/apiManage/apiVersionManage/enableOrDisableOperation")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "apiVersionId", value = "接口版本的Id",required = true,paramType = "query"),
		@ApiImplicitParam(name = "status", value = "接口状态：ENABLE(启用):DISABLE(禁用)",paramType = "query")
	})
	public Result enableOrDisableOperation(@RequestParam Integer apiVersionId,@RequestParam String status){
		if(apiVersionId==null ||StringUtils.isBlank(status)) {
			return Result.failure("启用,禁用操作的时候apiVersionId,status不能为空");
		}
		try{
			 apiVersionManageService.enableOrDisableOperation(apiVersionId,status);
			return new Result(true,"操作成功");
		}catch (Exception e){
			log.error("操作失败", e);
			return new Result(false, "操作失败");
		}
	}

}
